/*
 * Address.java
 *
 * Created on 22. květen 2007, 22:35
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package servissimulator;

import java.io.FileNotFoundException;

import java.io.*;

import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.NodeList;



/**
 *
 * @author jirka
 */
public class Address {
    private Random random;
    private String xmlFileInput = "data/ulice.xml";
    
    /** Creates a new instance of Address */
    public Address() {
         random = new Random();
    }
      /**
     *@return ulice 
     */
     public String getRandomStreet(){
                String  result=null;
		Document doc;
		DocumentBuilder docBuilder;
		DocumentBuilderFactory dBuFa = DocumentBuilderFactory.newInstance();
		try {
			docBuilder = dBuFa.newDocumentBuilder();
			doc = docBuilder.parse(this.xmlFileInput);
			NodeList l = doc.getElementsByTagName("ulice");
			NamedNodeMap map;
			if(l!=null){
				map = l.item(random.nextInt(l.getLength())).getAttributes();
				result = map.getNamedItem("nazev").getNodeValue();
			}
			
			//System.out.println("XML file parsed");
		} //TODO
		catch(FileNotFoundException e){
			System.out.println("File not found..exiting");
			System.exit(1);
		}
		
		catch (Exception e) {
			System.out.println(e.getMessage());
			result = "";
		}
	return result;	
	}
        /**
     *@return random zip code 
     */
       public String getRandomZIPCode(){
                String  result=null;
		Document doc;
		DocumentBuilder docBuilder;
		DocumentBuilderFactory dBuFa = DocumentBuilderFactory.newInstance();
		try {
			docBuilder = dBuFa.newDocumentBuilder();
			doc = docBuilder.parse(this.xmlFileInput);
			NodeList l = doc.getElementsByTagName("obec");
			NamedNodeMap map;
			if(l!=null){
				map = l.item(random.nextInt(l.getLength())).getAttributes();
				result = map.getNamedItem("MinPSC").getNodeValue();
			}
			
			//System.out.println("XML file parsed");
		} //TODO
		catch(FileNotFoundException e){
			System.out.println("File not found..exiting");
			System.exit(1);
		}
		
		catch (Exception e) {
			System.out.println(e.getMessage());
			result = "";
		}
        
      String [] temp = null;
      temp = result.split(" ");
        result=temp[0]+temp[1];
	return result;	
	}
   
	
	
    


}